using System;
using System.Threading;

namespace Kugar.Core.Communications.Transfer
{
    internal class RUDPConnectAsyncResult : IAsyncResult
    {
        private AsyncCallback _callback = null;
        private object _state = null;
        private int _connectID = 0;
        private int _bagID = 0;
        private Exception _error = null;
        private RUDPServer _server=null;


        public RUDPConnectAsyncResult(RUDPServer server, int bagID, AsyncCallback callback, object state)
        {
            _callback = callback;
            _state = state;
            _bagID = bagID;
            _server = server;
        }

        internal void Execute(Exception error=null)
        {
            _error = error;
            ThreadPool.QueueUserWorkItem((o) =>
                                         {
                                             if (_callback != null)
                                             {
                                                 _callback((IAsyncResult)o);
                                             }
                                         }, this);

        }

        internal int ConnectID
        {
            get { return _connectID; }
            set { _connectID = value; }
        }

        internal int BagID { get { return _bagID; } }

        internal Exception Error{get { return _error; }}

        internal bool HasError
        {
            get { return _error != null; }
        }

        internal RUDPServer Server { get { return _server; } }

        #region Implementation of IAsyncResult

        public bool IsCompleted
        {
            get { throw new NotImplementedException(); }
            internal set { }
        }


        public WaitHandle AsyncWaitHandle
        {
            get { throw new NotImplementedException(); }
        }

        public object AsyncState
        {
            get { return _state; }
        }

        public bool CompletedSynchronously
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}